/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package eshop

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *ShopCate) RecodeById(id int64) *ShopCate {
	item := new(ShopCate)
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if err != nil || item.Id <= 0 {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *ShopCate) AddRecode(item2add *ShopCate) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopCate) AddRecodeWithsession(session *xorm.Session, item2add *ShopCate) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *ShopCate) DelRecodeById(id int64) bool {
	item := new(ShopCate)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopCate) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(ShopCate)
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 更新
 * -----------------*/
func (t *ShopCate) UpdateRecode(rc *ShopCate) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *ShopCate) UpdateRecodeWithSession(session *xorm.Session, rc *ShopCate) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

//type CateItem struct {
//	Id   string
//	CateName string
//}

func (t *ShopCate)  GetShopCates() []*ShopCate  {
	var cateList []*ShopCate = []*ShopCate{}
	session := lego.GetDBEngine().Table("tb_shop_cate").Where("if_del = 0").Alias("t1")
	err := session.Find(&cateList)
	if err != nil {
		lego.LogError(err)
		return cateList
	}
	return cateList
}
